package chapter;

/**
 * string字符最大长度 测试
 */
public class Length {
    public static void main(String[] args) {
        int low = 0;//最小点
        int high = Integer.MAX_VALUE;//最大点
        int mid = (low+high)/2;//中间点
        int max = low;
        while(low<=high){
            //尝试十次
            for(int i=1;i<=10;i++){
                try {
                    char[] c = new char[mid];
                    //成功 将值赋值给max,继续尝试更大的值
                    max=mid;
                    low = mid+1;
                    mid = (high+low)/2;
                    System.out.println(i+"次申请成功："+max);
                    break;
                }catch (OutOfMemoryError e){
                    if(i==10){
                        System.out.println("申请失败："+mid);
                        high = mid-1;
                        mid = (high+low)/2;
                    }
                }
            }
        }
        System.out.println("可申请的最大值："+max);
    }
}
